package logic

import (
	"activity_srv/activity"
	"activity_srv/internal/dao"
	"activity_srv/internal/svc"
	"context"
	"fmt"
	"go.uber.org/zap"
	"smart-health-platforms/pkg"
	"smart-health-platforms/untils"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateSeckillOrderLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateSeckillOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateSeckillOrderLogic {
	return &CreateSeckillOrderLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CreateSeckillOrderLogic) CreateSeckillOrder(in *activity.CreateSeckillOrderRequest) (*activity.CreateSeckillOrderResponse, error) {
	alipay := untils.AliPay{}

	order, err := dao.CreateSeckillOrder(in)
	if err != nil {
		pkg.Error("创建秒杀订单失败", zap.Error(err))
		return nil, err
	}

	return &activity.CreateSeckillOrderResponse{
		OrderSn: order.OrderSn,
		PayUrl:  alipay.AliPay(order.OrderSn, fmt.Sprintf("%.2f", order.SeckillPrice)),
	}, nil
}
